package com.ruoyi.test.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.test.domain.InterfaceManagement;
import com.ruoyi.test.domain.ParamEdit;
import com.ruoyi.test.service.IParamEditService;
import com.ruoyi.test.service.IdataInsertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/test/param")
public class ParamEditController extends BaseController {

    @Autowired
    private IParamEditService paramEditService;

    @Autowired
    private IdataInsertService dataInsertService;

    /**
     * 查询接口管理列表
     */
//    @PreAuthorize("@ss.hasPermi('test:management:list')")
    @GetMapping(value = "/{id}")
//    public int get(@PathVariable("id") Long id)
//    {
//        return paramEditService.tableExist(id);
//    }
    public TableDataInfo list(@PathVariable("id") Long id)
    {
//        startPage();
        int tableExist = paramEditService.tableExist(id);
        if (tableExist == 0){
           paramEditService.createTable(id);
        }
        List<ParamEdit> list = paramEditService.selectParamEditList(id);
        return getDataTable(list);
    }
    @GetMapping(value = "/getTable/{id}")
    public void get(@PathVariable("id") Long id)
    {
        dataInsertService.handleJson(id);
    }
    /**
     * 新增接口管理
     */
    @PreAuthorize("@ss.hasPermi('test:management:add')")
    @PostMapping
    public AjaxResult add(@RequestBody ParamEdit paramEdit)
    {
       paramEditService.insertParam(paramEdit);
        return  AjaxResult.success();
    }

    /**
     * 修改接口管理
     */
    @PreAuthorize("@ss.hasPermi('test:management:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody ParamEdit paramEdit)
    {
        paramEditService.updateParam(paramEdit);
        return  AjaxResult.success();
//        return toAjax(paramEditService.updateParam(paramEdit));
    }


    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('test:management:remove')")
    @DeleteMapping
    public AjaxResult remove(@RequestBody ParamEdit paramEdit)
    {
        paramEditService.deleteParam(paramEdit);
        return  AjaxResult.success();
    }
}
